let express = require("express");
let https = require("https");
let request = require("request");
var router = express.Router();
const pool = require("../pool");
var Base64 = require("js-base64").Base64;
const jwt = require("../jwt");

//人脸识别接口
var token =
  "24.3537844202e96af4553fb1b5dd8940fd.2592000.1637737068.282335-25049021";

//人脸检测的接口
router.post("/face", (req, res) => {
  let options = {
    host: "aip.baidubce.com",
    path: '/rest/2.0/face/v3/detect?access_token="' + token + '"',
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  let str = req.body.img;
  console.log(str);
  let contents = JSON.stringify({
    // url  图片类型
    image: str,
    image_type: "BASE64",
    face_field: "age,beauty,expression,face_shape,gender,glasses,race",
    //这里根据文档填入你想要获取到的参数
  });

  let req_baiduai = https.request(options, function (res_baiduai) {
    res_baiduai.setEncoding("utf8");
    res_baiduai.on("data", function (chunk) {
      res.send(chunk);
      var testResult = JSON.parse(chunk);
      console.log(chunk);
      res.end();
    });
  });
  req_baiduai.write(contents);
  req_baiduai.end();
});

// 人脸注册接口

router.post("/addface", (req, res) => {
  let options = {
    host: "aip.baidubce.com",
    path: '/rest/2.0/face/v3/faceset/user/add?access_token="' + token + '"',
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };

  let { img, uid } = req.body;

  console.log(img);
  let contents = JSON.stringify({
    // url  图片类型
    image: img,
    image_type: "BASE64",
    group_id: "group6",
    user_id: uid,
  });

  let req_baiduai = https.request(options, function (res_baiduai) {
    res_baiduai.setEncoding("utf8");
    res_baiduai.on("data", function (chunk) {
      res.send(chunk);
      var testResult = JSON.parse(chunk);
      console.log(chunk);
      res.end();
    });
  });
  req_baiduai.write(contents);
  req_baiduai.end();
});

// 人脸搜索接口
router.post("/seachface", (req, res) => {
  let options = {
    host: "aip.baidubce.com",
    path: '/rest/2.0/face/v3/search?access_token="' + token + '"',
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  let str = req.body.img;
  console.log(Base64.decode(str));
  let contents = JSON.stringify({
    // url  图片类型
    image: str,
    image_type: "BASE64",
    group_id_list: "group6",
  });

  let req_baiduai = https.request(options, function (res_baiduai) {
    res_baiduai.setEncoding("utf8");
    res_baiduai.on("data", function (chunk) {
      res.send(chunk);
      var testResult = JSON.parse(chunk);
      console.log(chunk);
      res.end();
    });
  });
  req_baiduai.write(contents);
  req_baiduai.end();
});

// 人脸登录接口
router.post("/logonface", (req, res) => {
  let { uphone } = req.body;
  let sql = "select * from  user_information where wphone =?;";
  pool.query(sql, [uphone], (err, result) => {
    if (err) throw err;
    if (result.length != 0) {
      res.send({
        message: "登录成功",
        code: 200,
        result: result,
        token: jwt.generateToken(result[0]),
      });
    } else {
      res.send({
        message: "登录失败",
        code: 201,
      });
    }
  });
});

// 在线图片活体监测
router.post("/faceactive", (req, res) => {
  let options = {
    host: "aip.baidubce.com",
    path: '/rest/2.0/face/v3/faceverify?access_token="' + token + '"',
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
  };
  let str = req.body.img;
  console.log(str);
  // console.log(Base64.decode(str));
  let contents = JSON.stringify([
    {
      // url  图片类型
      image: str,
      image_type: "BASE64",
      face_field: "age,beauty,spoofing",
      option: "COMMON",
    },
  ]);
  let rawData = "";
  let req_baiduai = https.request(options, function (res_baiduai) {
    res_baiduai.setEncoding("utf8");
    res_baiduai.on("data", function (chunk) {
      rawData = `${rawData}${chunk}`;
    }),
      res_baiduai.on("end", function () {
        res.send(rawData);
        var testResult = JSON.parse(rawData);
        console.log(rawData);
        res.end();
      });
  });
  req_baiduai.write(contents);
  req_baiduai.end();
});

// 手机验证码接口

module.exports = router;
